package applet.admin.components.events;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import data.CMSRes;
import data.event.Event;
import data.event.EventTable;

import applet.misc.action.EnableActionListener;
import applet.misc.graphics.AdminBackgroundPanel;

public class CMSAdminEventsView extends AdminBackgroundPanel
{
	private static final long serialVersionUID = -3151733046481336870L;
	
	private EventTable eventTable;
	private JButton createButton;
	private JButton deleteButton;
	private JButton editButton;
	private JButton saveButton;

	public CMSAdminEventsView()
	{
		super(new GridBagLayout());
		setBorder(new EmptyBorder(25, 25, 25, 25));
		
		defineComponents();
		JPanel eventsDialog = new JPanel(new BorderLayout());
		eventsDialog.setBorder(new BevelBorder(BevelBorder.RAISED));
		
				JPanel titlePane = new JPanel(new BorderLayout());
				titlePane.setBackground(CMSRes.Colors.get("Admin Color Medium"));
				titlePane.setBorder(new EmptyBorder(10, 10, 10, 10));
					JLabel title = new JLabel("Conference Events:");
					title.setFont(title.getFont().deriveFont(Font.BOLD));
					titlePane.add(title, BorderLayout.WEST);
				eventsDialog.add(titlePane, BorderLayout.NORTH);
				
			eventsDialog.add(getContentPanel(), BorderLayout.CENTER);
			eventsDialog.add(getSaveBar(), BorderLayout.SOUTH);
		add(eventsDialog);
	}
	
	private void defineComponents()
	{
		saveButton = new JButton("Save Events");
		saveButton.setFont(saveButton.getFont().deriveFont(Font.BOLD));
		saveButton.setPreferredSize(new Dimension(200, 30));
		saveButton.addActionListener(new SaveListener());
		saveButton.addActionListener(new EnableActionListener(saveButton, false));
		
		createButton = new JButton("New Event");
		createButton.setFont(createButton.getFont().deriveFont(Font.BOLD));
		createButton.setPreferredSize(new Dimension(200, 30));
		createButton.addActionListener(new CreateListener());
		
		deleteButton = new JButton("Delete Event");
		deleteButton.setFont(deleteButton.getFont().deriveFont(Font.BOLD));
		deleteButton.setPreferredSize(new Dimension(200, 30));
		deleteButton.addActionListener(new DeleteListener());
		
		editButton = new JButton("Edit Event");
		editButton.setFont(deleteButton.getFont().deriveFont(Font.BOLD));
		editButton.setPreferredSize(new Dimension(200, 30));
		editButton.addActionListener(new EditListener());
		
		ArrayList<Event> eventList = new ArrayList<Event>();
		eventList.addAll(CMSRes.conference().events());
		eventTable = new EventTable(eventList);
		//eventTable.setPreferredSize(new Dimension(500, 300));
	}
	
	private JPanel getContentPanel()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.LOWERED), 
				new EmptyBorder(5, 5, 5, 5)));
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setPreferredSize(new Dimension(500, 300));
			layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
			layoutHelper.add(new JScrollPane(eventTable), BorderLayout.CENTER);
		panel.add(layoutHelper, BorderLayout.CENTER);
			layoutHelper = new JPanel(new GridLayout(1, 0));
				JPanel layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(5, 5, 5, 5));
				layout2.add(createButton, BorderLayout.CENTER);
			layoutHelper.add(layout2);
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(5, 5, 5, 5));
				layout2.add(editButton, BorderLayout.CENTER);
			layoutHelper.add(layout2);
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(5, 5, 5, 5));
				layout2.add(deleteButton, BorderLayout.CENTER);
			layoutHelper.add(layout2);
		panel.add(layoutHelper, BorderLayout.SOUTH);
		return panel;
	}
	
	private JPanel getSaveBar()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBackground(CMSRes.Colors.get("Admin Color Medium"));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(saveButton, BorderLayout.EAST);
		return panel;
	}
	
	private class SaveListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e)
		{
			CMSRes.conference().events().clear();
			CMSRes.conference().events().addAll(eventTable.events());
			CMSRes.conference().saveData();
		} }
	
	private class EditListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e)
		{
			Event event = eventTable.getSelected();
			if(event != null)
			{
				saveButton.setEnabled(true);
				EventEditDialog dialog = new EventEditDialog(event, eventTable);
				dialog.setLocationRelativeTo(null);
				dialog.setVisible(true);
			}
		} }
	
	private class CreateListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e)
		{
			EventCreateDialog dialog = new EventCreateDialog(eventTable);
			dialog.setLocationRelativeTo(null);
			dialog.setVisible(true);
			saveButton.setEnabled(true);
		} }
	
	private class DeleteListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e)
		{
			String[] options = new String[] { "Cancel", "Delete Event" };
			int n = JOptionPane.showOptionDialog(CMSRes.frame(),
					"Once deleted, this event will be unrecoverable. " +
					"Are you sure you wish to delete it?",
				    "Delete Event?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
				    null, options, options[0]);
			if(n == 1)
			{
				Event event = eventTable.getSelected();
				if(event != null)
				{
					eventTable.removeEvent(event);
					saveButton.setEnabled(true);
				}
			}
		} }
}